//
//  MakefirendsDetailViewController.m
//  cityStar
//
//  Created by joyban on 15-1-21.
//  Copyright (c) 2015年 tiny. All rights reserved.
//

#import "MakefirendsDetailViewController.h"
#import "SGFocusImageFrame.h"
#import "SGFocusImageItem.h"
#import "UIColor+FlatUI.h"

#import "MakefirendsdetailCell.h"
#import "RIButtonItem.h"
#import "UIAlertView+Blocks.h"
NSString *const MakefirendsdetailCellID = @"makefirendsdetailCellID";


@interface MakefirendsDetailViewController ()<SGFocusImageFrameDelegate>

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *addressLine;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *labeliftLine;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *ageLabel;
@property (weak, nonatomic) IBOutlet UILabel *sexLabel;
@property (weak, nonatomic) IBOutlet UILabel *addressLabel;
@property (weak, nonatomic) IBOutlet UILabel *hujiLabel;
@property (weak, nonatomic) IBOutlet UILabel *degreeLabel;
@property (weak, nonatomic) IBOutlet UILabel *careerLAbe;
@property (weak, nonatomic) IBOutlet UILabel *marital_statusLabel;
@property (weak, nonatomic) IBOutlet UILabel *car_houseLabel;
@property (weak, nonatomic) IBOutlet UILabel *salaryLabel;
@property (weak, nonatomic) IBOutlet UIButton *Consumptionbtn;

@property (strong, nonatomic) IBOutlet UIView *heardView;
@property (weak, nonatomic) IBOutlet UIView *scrolerlView;
@property (weak, nonatomic) IBOutlet UITableView *MakefirendsDetailView;
@property (strong,nonatomic) SGFocusImageFrame *heardTableView;
@property (weak, nonatomic) IBOutlet UILabel *QQLabel;
@property (weak, nonatomic) IBOutlet UILabel *weixinLabel;

@end

@implementation MakefirendsDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"交友详情";
    [self addBackButton];
    [self setUpTableViews];
    [self setHeardTableView];
    [self setdataHeardcontent];
    if([self.withchType isEqualToString:@"cityService"])
    {
        [self.Consumptionbtn setTitle:@"消费积分查看联系方式" forState:UIControlStateNormal];
    }else{
        [self.Consumptionbtn setTitle:@"信息作废" forState:UIControlStateNormal];
        [self.Consumptionbtn setTitle:@"信息作废" forState:UIControlStateHighlighted];

        [self.Consumptionbtn setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)setdataHeardcontent
{
    self.titleLabel.text = self.freienModel.title;
    self.ageLabel.text = [NSString stringWithFormat:@"%@岁",self.freienModel.age];

    self.addressLabel.text = self.freienModel.district_name;
    
    self.hujiLabel.text = self.freienModel.census;
    if(self.hujiLabel.text.length > 2)
    {
        self.addressLine.constant = 220;
    }
    
    NSInteger sexstring = [[NSString stringWithFormat:@"%@",self.freienModel.gender] integerValue];
    switch (sexstring) {
        case 0:
            self.sexLabel.text = @"女";
            break;
        case 1:
            self.sexLabel.text = @"男";
            break;
        case 2:
            self.sexLabel.text = @"男";
            break;
        case 4:
            self.sexLabel.text = @"女";
            break;
        default:
            break;
    }
    
    NSInteger leinStr = [[NSString stringWithFormat:@"%@",self.freienModel.degree] integerValue];
    switch (leinStr) {
        case 0:
            self.degreeLabel.text = @"不限";
            break;
        case 1:
            self.degreeLabel.text = @"初中";
            break;
        case 2:
            self.degreeLabel.text = @"高中";
            break;
        case 3:
            self.degreeLabel.text = @"专专";
            break;
        case 4:
            self.degreeLabel.text = @"大专";
            break;
        case 5:
            self.degreeLabel.text = @"本科";
            break;
        case 6:
            self.degreeLabel.text = @"硕士";
            break;
        case 7:
            self.degreeLabel.text = @"博士";
            break;
        case 8:
            self.degreeLabel.text = @"留学";
            break;
        default:
            break;
    }
    self.careerLAbe.text = self.freienModel.career;
    
    NSInteger moneyStr = [[NSString stringWithFormat:@"%@",self.freienModel.salary] integerValue];
    switch (moneyStr) {
        case 1:
            self.salaryLabel.text = @"3000以下";
            break;
        case 2:
            self.salaryLabel.text = @"3000-5000元/月";
            break;
        case 3:
            self.salaryLabel.text = @"5000-8000元/月";
            break;
        case 4:
            self.salaryLabel.text = @"8000-15000元/月";
            break;
        case 5:
            self.salaryLabel.text = @"15000-20000元/月";
            break;
        case 6:
            self.salaryLabel.text = @"20000-40000元/月";
            break;
        case 7:
            self.salaryLabel.text = @"40000-100000元/月";
            break;
        case 8:
            self.salaryLabel.text = @"100000+";
            break;
        case 9:
            self.salaryLabel.text = @"我有公司";
            break;
        default:
            break;
    }
    
    NSInteger housestring = [[NSString stringWithFormat:@"%@",self.freienModel.car_house] integerValue];
    switch (housestring) {
        case 0:
            self.car_houseLabel.text = @"无房无车";
            break;
        case 1:
            self.car_houseLabel.text = @"有车无房";
            break;
        case 2:
            self.car_houseLabel.text = @"有房有车";
            break;
        default:
            break;
    }
    
    NSInteger maritalstring = [[NSString stringWithFormat:@"%@",self.freienModel.marital_status] integerValue];
    switch (maritalstring) {
        case 0:
            self.marital_statusLabel.text = @"未婚";
            break;
        case 1:
            self.marital_statusLabel.text = @"已婚";
            break;
        case 2:
            self.marital_statusLabel.text = @"离异";
            self.labeliftLine.constant = 45;
            break;
        case 3:
            self.marital_statusLabel.text = @"丧偶";
            break;
        default:
            break;
    }
    NSString * qq = [NSString stringWithFormat:@"%@",self.freienModel.qq];
    if([qq isEqualToString:@"(null)"])
    {
        self.QQLabel.text = @"暂无QQ";
    }else{
        self.QQLabel.text = qq;
    }
    NSString *weixin = [NSString stringWithFormat:@"%@",self.freienModel.wechar];
    if([weixin isEqualToString:@"(null)"])
    {
        self.weixinLabel.text = @"暂无微信";
    }else
    {
        self.weixinLabel.text = weixin;
    }
}

-(void)setHeardTableView
{
    NSMutableArray *tempArray = [NSMutableArray array];
    __block NSInteger length;
    if (self.freienModel.image.count > 0) {
        [self.freienModel.image enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL *stop) {
            NSString *strimagepath = [NSString stringWithFormat:@"http://api.icszx.com/image/c/id/%@/w/640/h/300/m/1",self.freienModel.image[idx]];
            NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:strimagepath,@"imagePath",nil];
            length = tempArray.count+1;
            [tempArray addObject:dict];
        }];
    }else{
         length = 1;
        for (int i = 0 ; i < length; i++)
        {
            NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"banner",@"imagePath",nil];
            [tempArray addObject:dict];
        }
    }
    //添加最后一张图 用于循环
    NSMutableArray *itemArray = [NSMutableArray arrayWithCapacity:length+2];
    if (length > 1)
    {
        NSDictionary *dict = [tempArray objectAtIndex:length-1];
        SGFocusImageItem *item = [[SGFocusImageItem alloc] initWithDict:dict tag:-1] ;
        [itemArray addObject:item];
    }
    for (int i = 0; i < length; i++)
    {
        NSDictionary *dict = [tempArray objectAtIndex:i];
        SGFocusImageItem *item = [[SGFocusImageItem alloc] initWithDict:dict tag:i];
        [itemArray addObject:item];
    }
    //添加第一张图 用于循环
    if (length >1)
    {
        NSDictionary *dict = [tempArray objectAtIndex:0];
        SGFocusImageItem *item = [[SGFocusImageItem alloc] initWithDict:dict tag:length];
        [itemArray addObject:item];
    }
    self.heardTableView = [[SGFocusImageFrame alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 150) delegate:self imageItems:itemArray isAuto:NO];
    [self.heardTableView scrollToIndex:2];
    [self.scrolerlView addSubview: self.heardTableView];
    self.MakefirendsDetailView.tableHeaderView = self.heardView;
}

-(void)foucusImageFrame:(SGFocusImageFrame *)imageFrame didSelectItem:(SGFocusImageItem *)item ImagePage:(int)pageIndex
{
    
}

-(void)setUpTableViews
{
    [self.MakefirendsDetailView registerNib:[MakefirendsdetailCell nib] forCellReuseIdentifier:MakefirendsdetailCellID];
}

#pragma mark - Actions
-(IBAction)getContact:(id)sender
{
    if ([self.withchType isEqualToString:@"cityService"]) {
        NSString *postUrl = [NSString stringWithFormat:@"%@%@/%i",KinterFace_PublishContact,self.freienModel._id,[self.freienModel.type intValue]];
        [HttpRequestManager getMethod:postUrl parameters:nil completedMainBlock:^(BOOL success, id response, NSString *errMsg) {
            if (success) {
                DLog(@"%@",response);
                if(![self.Consumptionbtn.titleLabel.text isEqualToString:@"消费积分查看联系方式"])
                {
                    NSString *string = [NSString stringWithFormat:@"%@",response[@"value"][@"contacts"][@"phone"]];
                    NSString *telStr=[@"tel://" stringByAppendingString:string];
                    NSURL *url=[NSURL URLWithString:telStr];
                    [[UIApplication sharedApplication]openURL:url];
                }else{
                    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"是否消耗积分查看联系方式" message:nil delegate:self cancelButtonTitle:@"是" otherButtonTitles:@"否",nil];
                    [alertView show];
                }
            }else{
                [SVProgressHUD showErrorWithStatus:errMsg];
            }
        }];
    }else{
    RIButtonItem *itemCancel = [RIButtonItem itemWithLabel:@"取消"];
    RIButtonItem *itemSubMit = [RIButtonItem itemWithLabel:@"确定" action:^{
        NSString *postUrl = [NSString stringWithFormat:@"%@%@",KinterFace_PublishInvalid,self.freienModel._id];
        [HttpRequestManager deleteMethod:postUrl parameters:nil completedMainBlock:^(BOOL success, id response, NSString *errMsg) {
            if (success) {
                [SVProgressHUD showSuccessWithStatus:@"信息作废成功"];
            }else{
                [SVProgressHUD showErrorWithStatus:@"信息作废失败"];
            }
        }];
    }];
    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"是否作废该信息" message:nil cancelButtonItem:itemCancel otherButtonItems:itemSubMit, nil];
    [alertView show];
}
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
{
    if (buttonIndex == 0) {
        NSString *postUrl = [NSString stringWithFormat:@"%@%@/%i",KinterFace_PublishContact,self.freienModel._id,[self.freienModel.type intValue]];
        [HttpRequestManager getMethod:postUrl parameters:nil completedMainBlock:^(BOOL success, id response, NSString *errMsg) {
            if (success) {
                DLog(@"%@",response);
                NSString *string = [NSString stringWithFormat:@"%@",response[@"value"][@"contacts"][@"phone"]];
                
                NSString *title = [NSString stringWithFormat:@"%@:%@",self.freienModel.contact,string];
                [self.Consumptionbtn setTitle:title forState:UIControlStateNormal];
            }else{
                [SVProgressHUD showErrorWithStatus:errMsg];
            }
        }];
    }
}

#pragma mark - UITableViewDelegate
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *ss = [NSString stringWithFormat:@"%@",self.freienModel.desc];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 220, 25)];
    label.font = [UIFont systemFontOfSize:14];
    [label setLineBreakMode:NSLineBreakByWordWrapping];
    [label setNumberOfLines:0];
    NSString *_Detail = [NSString stringWithFormat:@"%@",ss];
    label.text = _Detail;
    [label sizeToFit];
    float labelheight = label.frame.size.height;
    return [MakefirendsdetailCell heightOfTableCell] - (80 - labelheight);
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 25;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UILabel * label = [[UILabel alloc] init] ;
    label.frame = CGRectMake(15, 0, 200, 25);
    label.backgroundColor = [UIColor clearColor];
    label.font=[UIFont systemFontOfSize:14];
    label.textColor = [UIColor colorFromHexCode:@"949494"];
    
    if (section == 0)
    {
        label.text = @"择偶要求";
    }else{
        label.text = @"自我介绍";
    }
    UIView * sectionView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 220)] ;
    [sectionView setBackgroundColor:[UIColor colorFromHexCode:@"FFF3E2"]];
    [sectionView addSubview:label];
    return sectionView;

}

#pragma mark - UITableViewDataSource
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MakefirendsdetailCell *ListdetailCell = [tableView dequeueReusableCellWithIdentifier:MakefirendsdetailCellID];
    ListdetailCell.selectionStyle = UITableViewCellSelectionStyleNone;
    if(indexPath.section == 0)
    {
        ListdetailCell.contentLabel.text = self.freienModel.require;
    }else{
        ListdetailCell.contentLabel.text = self.freienModel.desc;

    }
    return ListdetailCell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
